package com.xgq.drink.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xgq.drink.entity.ChatMessage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 聊天消息Mapper接口
 */
public interface ChatMessageMapper extends BaseMapper<ChatMessage> {
        @Select("SELECT cm.* " +  // 移除了附件字段选择
                "FROM chat_message cm " +  // 移除了LEFT JOIN
                "WHERE ((cm.sender_id = #{senderId} AND cm.receiver_id = #{receiverId}) " +
                "OR (cm.sender_id = #{receiverId} AND cm.receiver_id = #{senderId})) " +
                "ORDER BY cm.send_stamp DESC " +
                "LIMIT #{limit} OFFSET #{offset}")
        List<ChatMessage> getPrivateChatHistory(@Param("receiverId") String receiverId,
                                                @Param("senderId") String senderId,
                                                @Param("offset") int offset,
                                                @Param("limit") int limit);

        @Select("SELECT * FROM chat_message " +
                        "WHERE group_id = #{groupId} " +
                        "ORDER BY send_stamp DESC " +
                        "LIMIT #{limit} OFFSET #{offset}")
        List<ChatMessage> getGroupChatHistory(@Param("groupId") String groupId,
                        @Param("limit") int limit,
                        @Param("offset") int offset);
        /**
         * 标记消息为已读
         * 
         * @param messageId 消息ID
         * @return 更新结果
         */
        @Update("UPDATE chat_message SET status = 1 WHERE message_id = #{messageId}")
        int markMessageAsRead(@Param("messageId") Long messageId);

        /**
         * 标记用户的所有未读消息为已读
         * 
         * @param receiverId 接收者ID
         * @param senderId   发送者ID
         * @return 更新结果
         */
        @Update("UPDATE chat_message SET status = 1 WHERE receiver_id = #{receiverId} " +
                        "AND sender_id = #{senderId} AND status = 0")
        int markAllMessagesAsRead(@Param("receiverId") Long receiverId, @Param("senderId") Long senderId);
}